import re
import os


def main():
    ctrl = True
    while(ctrl):
        menu()
        option = input("请选择:")
        option_str = re.sub("\D","",option)
        if option_str in ['0','1','2','3','4','5','6','7']:
            option_int = int(option_str)
            if option_int == 0:
                print("您已退出学生管理信息系统！")
                ctrl = False
            elif option_int == 1:
                insert()
            elif option_int == 2:
                search()
            elif option_int == 3:
                delete()
            elif option_int == 4:
                modify()
            elif option_int == 5:
                sort()
            elif option_int == 6:
                total()
            elif option_int == 7:
                show()

def menu():
    # 输出菜单
    print('''
    1.录入学生信息
    2.查找学生信息
    3.删除学生信息
    4.修改学生信息
    5.排序
    6.统计学生总人数
    7.显示所有学生信息
    0.退出系统
    ''')
    
def insert():
    stdList = []
    mark = True
    while mark:
        id = input("请输入ID：")
        if not id:
            break
        name = input("请输入名字：")
        if not name:
            break
        try:
            english = int(input("请输入英文成绩："))
            python = int(input("请输入python成绩："))
        except:
            print("输入无效，不是整数，请重新输入：")
            continue
        #将输入大的学生信息保存到字典中
        student = {"id":id,"name":name,"english":english,"python":python}
        stdList.append(student)
        inputMark = input("是否继续添加？(y/n)：")
        if inputMark == "y":
            mark = True
        else:
            mark = False
    save(stdList)
    print("信息录入完毕：")

def delete():
    mark = True
    while mark:
        stdId = input("需要删除的学生ID: ")
        if stdId != "":
            if os.path.exists(filename):
                with open(filename,'r') as rfile:
                    student_old = rfile.readlines()
            else:
                student_old = []
            ifdel = False
            if student_old:
                with open(filename,'w') as wfile:
                    d = {}
                    for list in student_old:
                        d = dict(eval(list))
                        if d['id'] != stdId:
                            wfile.write(str(d)+"\n")
                        else:
                            ifdel = True
                    if ifdel:
                        print("ID为 %s 的学生信息已经被删除" % stdId)
                    else:
                        print("没有找到ID为 %s 的学生信息"% stdId)
            else:
                print("无该学生信息")
                break
            show()
            inputMark = input("是否继续添加？(y/n)：")
            if inputMark == "y":
                mark = True
            else:
                mark = False

def modify():
    show()
    if os.path.exists(filename):
        with open(filename,'r') as rfile:
            student_old = rfile.readlines()
    else:
        return
    studentid = input("请输入要修改的学生ID：")
    with open(filename,'w') as wfile:
        for student in student_old:
            d = dict(eval(student))
            if d["id"] == studentid:
                print("找到了这名学生，可以修改他的信息")
                while True:
                    try:
                        d["name"] = input("请输入姓名：")
                        d["english"] = input("请输入英语成绩：")
                        d["python"] = input("请输入python成绩：")
                    except:
                        print("您的输入有误，请重新输入。")
                    else:
                        break
                student = str(d)
                wfile.write(student + "\n")
                print("修改成功！")
            else:
                wfile.write(student)
    mark = input("是否亟须修改其他学生信息？(y/n):")
    if mark == "y":
        modify()

def search():
    mark = True
    student_query = []
    while mark:
        id = ""
        name = ""
        if os.path.exists(filename):
            mode = input("按ID查输入1；按姓名查输入2：")
            if mode == "1":
                id = input("请输入学生ID：")
            elif mode == "2":
                name = input("请输入学生姓名：")
            else:
                print("您输入有误，请重新输入！")
                search()
            with open(filename,'r') as file:
                student = file.readlines()
                for list in student:
                    d = dict(eval(list))
                    if id != "":
                        if d['id'] == id:
                            student_query.append(d)
                    elif name != "":
                        if d['name'] == id:
                            student_query.append(d)
                    show_student(student_query)
                    student_query.clear()
                    inputMark = input("是否继续添加？(y/n)：")
                    if inputMark == "y":
                        mark = True
                    else:
                        mark = False
                else:
                    print("暂未保存数据信息")
                    return

def show_student(stdList):
    if not stdList:
        print("无数据信息")
        return
    format_title = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}"
    print(format_title.format("ID","名字","英语成绩","python成绩","总成绩"))
    format_data = "{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}"
    for info in stdList:
        print(format_data.format(info.get("id"),info.get("name"),str(info.get("english")),str(info.get("python")),str(info.get("english")+info.get("python")).center(12)))

def total():
    if os.path.exists(filename):
        with open(filename,'r') as rfile:
            student_old = rfile.readlines()
            if student_old:
                print("一共有 %d 个学生"% len(student_old))
            else:
                print("还没有录入学生信息")
    else:
        print("未保存数据信息")

def show():
    student_new = []
    if os.path.exists(filename):
        with open(filename,'r') as rfile:
            student_old = rfile.readlines()
            for list in student_old:
                student_new.append(eval(list))
            if student_new:
                show_student(student_new)
    else:
        print("未保存数据信息")
        
def sort():
    show()
    if os.path.exists(filename):
        with open(filename,'r') as rfile:
            student_old = rfile.readlines()
            student_new = []
            for list in student_old:
                student_new.append(dict(eval(list)))
    else:
        return
    ascORdesc = input("请选择(0升序;1降序): ")
    if ascORdesc == "0":
        ascORdescBool = False
    elif ascORdesc == "1":
        ascORdescBool = True
    else:
        print("输入错误，请重新数据")
        sort()
    mode = input("请选择排序方式(1按英语成绩排序;2按python成绩排序;0按总成绩排序):")
    if mode == "1":
        student_new.sort(key=lambda x:x["english"],reverse=ascORdescBool)
    elif mode == "2":
        student_new.sort(key=lambda x:x["python"],reverse=ascORdescBool)
    elif mode == "0":
        student_new.sort(key=lambda x:x["english"]+x["python"],reverse=ascORdescBool)
    else:
        print("输入错误，请重新输入")
        sort()
    show_student(student_new)
    
main()
